<template>
	<view style="height: 100vh;">
		<input type="text" placeholder="请输入标题" v-model="messageItem.title">
		<view class="uni-textarea">
			<textarea placeholder="请输入内容" v-model="messageItem.content" />
		</view>
		<button type="primary" @click="submit">提交</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				messageItem: {
					title: '',
					content: '',
					name: 'mq',
					date: ''
				}
			}
		},
		methods: {
			submit() {
				uni.request({
					url: 'http://localhost:8090/freshmenapp/message/addone',
					method: 'POST',
					data: this.messageItem,
					success: res => {
						console.log(res.data);
						if (res.data.success === true) {
							uni.showToast({
								title: '发表成功',
								icon: "success"
							});
						}
					},
					fail: (err) => {
						uni.showToast({
							title: '网络错误',
							icon: 'error'
						});
					},
				})
			},
			getCurrentTime() {
				let now = new Date(); // 获取当前时间
				let year = now.getFullYear(); // 获取年份 
				let month = String(now.getMonth() + 1).padStart(2, '0');
				let day = String(now.getDate()).padStart(2, '0'); // 获取日期  
				this.messageItem.date = `${year}-${month}-${day}`;
			}
		},
		onLoad() {
			this.getCurrentTime()
		}
	}
</script>

<style>
	input {
		background-color: #eaeaea;
		margin: 0px 10px;
		padding: 8px 0;
	}

	.uni-textarea {
		background-color: #eaeaea;
		margin: 10px 10px;
	}

	button {
		width: 90%;
	}
</style>